/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.commodity.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wealth.commodity.entity.Category;
import com.wealth.commodity.repository.CategoryDao;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.vo.CategoryItem;
import com.wealth.commodity.vo.CategoryVO;

import iaas.service.impl.BaseServiceImpl;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class CategoryServiceImpl extends BaseServiceImpl<Category> implements ICategoryService{

	@Autowired
	private CategoryDao categoryDao;

	/**  查询商品分类 企业+状态  */
	@Override
	public List<Category> findByStatusAndCorpId(Integer status, Long corpId) {
		
		return categoryDao.findByStatusAndCorpId(status, corpId);
	}
	
	/** 查询商品分类 企业+市场+状态 */
	@Override
	public List<Category> findByCorpIdAndMarketIdAndStatus(Long corpId,
			Long marketId, Integer status) {
		return categoryDao.findByCorpIdAndMarketIdAndStatusOrderBySeqDesc(corpId, marketId, status);
	}

	/** 查询商品分类 企业+市场+状态+父节点 */
	@Override
	public List<Category> getCategoryByCorpIdAndMarketIdAndStatusAndParentIdIsNull(Long corpId, Long marketId,
			Integer status) throws Exception {
		return categoryDao.findByCorpIdAndMarketIdAndStatusAndPcategoryIdIsNull(corpId, marketId, status);
	}
	/** 查询商品分类 企业+市场+状态+父节点 */
	@Override
	public Category findByCorpIdAndMarketIdAndStatusAndPcategoryIdAndName(Long cropId,
			Long marketId, Integer status, Long pcategoryId,String name) throws Exception {
		
		return categoryDao.findByCorpIdAndMarketIdAndStatusAndPcategoryIdAndName(cropId,marketId,status,pcategoryId,name);
	}

	/** ---------------- 微信端 ---------------- */
	/** 惠生活商品分类父子列表*/
	@Override
	public List<CategoryVO> getCategoryForHuiLife(Long corpId, Long marketId, Integer status) throws Exception {
		List<CategoryVO> volist = new ArrayList<CategoryVO>();
		List<Category> categoryList = categoryDao.findByCorpIdAndMarketIdAndStatusOrderBySeqDesc(corpId, marketId, status);
		List<Category> pcategoryList = new ArrayList<Category>();
		
		//父节点
		for (Category category : categoryList) {
			if(category.getPcategory()==null){
				pcategoryList.add(category);
			}
		}
		//遍历子节点
		for (Category parent : pcategoryList) {
			CategoryVO vo = new CategoryVO();
			vo.setParent(new CategoryItem(parent.getId(),parent.getName()));
			List<CategoryItem> childList = new ArrayList<CategoryItem>();
			for (Category child : categoryList) {
				if(child.getPcategory()!=null && child.getPcategory().getId()==parent.getId()){
					childList.add(new CategoryItem(child.getId(), child.getName())); 
				}
			}
			vo.setChildList(childList);
			volist.add(vo);
		}
		
		
		return volist;
	}

	@Override
	public List<Category> getByPcategoryIdAndStatus(Long pcategoryId, int value) {
		
		
		return categoryDao.findByPcategoryIdAndStatus(pcategoryId,value);
	}

	@Override
	public List<Category> getByMarketIdAndStatusAndPcategoryIsNull(Long mid,
			Integer status) {
		
		return categoryDao.findByMarketIdAndStatusAndPcategoryIsNull(mid,status);
	}

	@Override
	public Category getCategoryByCorpIdAndMarketIdAndPcategoryIsNullAndStatusAndName(
			Long corpId, Long marketId, Integer status,String name) throws Exception {
		
		return categoryDao.findCategoryByCorpIdAndMarketIdAndPcategoryIsNullAndStatusAndName(corpId,marketId,status,name);
	}

	@Override
	public List<Category> findByStatusAndCorpIdAndMarketIdAndPcategoryIsNull(Integer value,
			Long corpid, Long marketId) {
	
		return categoryDao.findByStatusAndCorpIdAndMarketIdAndPcategoryIsNull(value,corpid,marketId);
	}

	@Override
	public boolean unique(String mName,Integer status, Long pcategoryId, Long marketId) 
			throws Exception {
		Category category = categoryDao.findByMarketIdAndPcategoryIdAndNameAndStatus(marketId, pcategoryId, mName, status);
		return (category==null)?true:false;
	}

	//PC 端商品分类查询
	@Override
	public List<CategoryVO> getCategoryByStatusAndCorpId(Integer status,
			Long corpId) {
		
		List<CategoryVO> volist = new ArrayList<CategoryVO>();
		List<Category> categoryList = categoryDao.findByCorpIdAndStatusAndPcategoryIdIsNull(corpId, status);
		List<Category> categoryChildList = categoryDao.findByCorpIdAndStatusAndPcategoryIdIsNotNull(corpId, status);
		List<Category> pcategoryList = new ArrayList<Category>();
		
		//父节点
		for (Category category : categoryList) {
			if(category.getPcategory()==null){
				pcategoryList.add(category);
			}
		}
		//遍历子节点
		for (Category parent : pcategoryList) {
			CategoryVO vo = new CategoryVO();
			vo.setParent(new CategoryItem(parent.getId(),parent.getName()));
			List<CategoryItem> childList = new ArrayList<CategoryItem>();
			for (Category child : categoryChildList) {
				if(child.getPcategory()!=null && child.getPcategory().getId()==parent.getId()){
					childList.add(new CategoryItem(child.getId(), child.getName())); 
				}
			}
			vo.setChildList(childList);
			volist.add(vo);
		}
		return volist;
	}

	


	
	
	
}
